﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;

namespace SharingCenter.Web.Classes.Message
{
    using SharingCenter.Web.Classes.enums;
    using System.Configuration;
    using System.Text;
    using SharingCenter.Web.Classes.Profiles;
    using System.Web.Security;
    using SharingCenter.Web.Classes.Account;
    using System.Net;

    public class SiteMailMessage : IMessage
    {

        #region IMessage Members

        public void SendMailMessage(Guid from, Guid to, MailMessage message, Dictionary<string, string> replacement)
        {
            throw new NotImplementedException();
        }

        public void OnMailMessageSent()
        {
            throw new NotImplementedException();
        }

        public void SendMailMessage(Guid from, Guid to, MailMessage message)
        {
            
        }

        public void SendSystemMail(string subject, string content, string toMail, Dictionary<string, string> replacement)
        {
            MailAddress from = new MailAddress(ConfigurationManager.AppSettings["AccountMailAddress"], ConfigurationManager.AppSettings["SiteName"], Encoding.UTF8);
            MailAddress to = new MailAddress(toMail);
            MailMessage m = new MailMessage(from, to);

            FormatMailAndSend(ref m, ref subject, ref content, ref replacement);
        }

        public void SendSystemMail(string subject, string content, List<MailAddress> toList, Dictionary<string, string> replacement)
        {
            MailAddress from = new MailAddress(ConfigurationManager.AppSettings["AccountMailAddress"], ConfigurationManager.AppSettings["SiteName"], Encoding.UTF8);
            MailMessage m = new MailMessage();
            m.From = from;
            foreach (MailAddress a in toList)
            {
                m.To.Add(a);
            }
            FormatMailAndSend(ref m, ref subject, ref content, ref replacement);
        }

        #endregion

        void FormatMailAndSend(ref MailMessage m, ref string subject, ref string content, ref Dictionary<string, string> replacement)
        {
            if (replacement != null)
            {
                foreach (KeyValuePair<string, string> k in replacement)
                {
                    string key = WebConsts.MAIL_PARAM_L_DELIMETER + k.Key + WebConsts.MAIL_PARAM_R_DELIMETER;
                    subject = subject.Replace(key, k.Value);
                    content = content.Replace(key, k.Value);
                }
            }
            m.IsBodyHtml = false;
            m.Subject = subject;
            m.SubjectEncoding = Encoding.UTF8;
            m.Body = content;
            m.BodyEncoding = Encoding.UTF8;
            m.ReplyTo = new MailAddress("noreply@" + ConfigurationManager.AppSettings["DomainName"]);

            SmtpClient client = new SmtpClient();
            client.Send(m);
        }
    }
}
